﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using Intergaces;

namespace DomainModel
{
    public class ImageService
    {
        //repository for access to DAL
        private IImageRepository repository = null;

        public ImageService(IImageRepository rep)
        {
            repository = rep;
        }

        public void SaveImage(ImageEntity image)
        {
            repository.Save(image);
        }

        public IEnumerable<IImage> LoadImage(int start, int end)
        {
            return repository.Load(start, end);
        }


				public IEnumerable<IImage> LoadIDs(int start, int end)
        {
            return repository.GetIDs(start, end);
        }

        public byte[] LoadImageFull(Guid id)
        {
            return repository.LoadImageFull(id);
        }

        public byte[] LoadImageLittle(Guid id)
        {
            return repository.LoadImageLittle(id);
        }


        public int GetImageCount()
        {
            return repository.GetImageCount();
        }

        public byte[] SetImageSize(Stream image, int height)
        {
            try
            {

                var img = Image.FromStream(image);


                if (img.Height > height)
                {
                    var width = (height * img.Width) / img.Height;
                    img = CropImage(img, width, height);
                }

                var memoryStream = new MemoryStream();
                img.Save(memoryStream, ImageFormat.Jpeg);
                return memoryStream.ToArray();

            }
            catch (Exception e)
            {
                //log
                throw new Exception("Set new size exeption",e);
            }
        }

        protected Image CropImage(Image sourceImage, int targetWidth, int targetHeight)
        {
            var sourceWidth = sourceImage.Width;
            var sourceHeight = sourceImage.Height;
            const int sourceX = 0;
            const int sourceY = 0;
            int targetX = 0, targetY = 0;

            float ratio;
            var ratioWidth = targetWidth / (float)sourceWidth;
            var ratioHeight = targetHeight / (float)sourceHeight;

            if (ratioHeight < ratioWidth)
            {
                ratio = ratioWidth;
                targetY = (int)((targetHeight - sourceHeight * ratio) / 2);
            }
            else
            {
                ratio = ratioHeight;
                targetX = (int)((targetWidth - sourceWidth * ratio) / 2);
            }

            var destWidth = (int)(sourceWidth * ratio);
            var destHeight = (int)(sourceHeight * ratio);
            var targetImage = new Bitmap(targetWidth, targetHeight, PixelFormat.Format24bppRgb);

            targetImage.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution);

            using (var g = Graphics.FromImage(targetImage))
            {
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawImage(
                        sourceImage,
                        new Rectangle(targetX, targetY, destWidth, destHeight),
                        new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                        GraphicsUnit.Pixel);
            }
            return targetImage;
        }



    }
}
